/*
 * @Author: Vue Sound Team
 * @Date: 2025-01-15
 * @Description: 百度地图API配置
 */

// 百度地图API配置
export const BAIDU_MAP_CONFIG = {
  // 请替换为你的百度地图API密钥
  // 申请地址: https://lbsyun.baidu.com/
  API_KEY: '4jcLEpFS2DDdGRjAWz3lG1aGvkcQzboq', // 替换为你的API Key
  
  // 地图默认配置
  DEFAULT_CENTER: {
    lng: 117.000923, // 山东省中心经度
    lat: 36.675807   // 山东省中心纬度
  },
  
  // 默认缩放级别
  DEFAULT_ZOOM: 7,
  
  // 缩放范围
  MIN_ZOOM: 5,
  MAX_ZOOM: 18,
  
  // 地图样式配置
  MAP_STYLES: {
    normal: 'BMAP_NORMAL_MAP',      // 标准地图
    satellite: 'BMAP_SATELLITE_MAP', // 卫星地图
    hybrid: 'BMAP_HYBRID_MAP'       // 混合地图
  }
}

// 山东省主要城市坐标数据
export const SHANDONG_CITIES = [
  { id: 1, name: '济南市', lng: 117.000923, lat: 36.675807, population: 920, area: 10244, gdp: 10140, features: '省会城市，泉城' },
  { id: 2, name: '青岛市', lng: 120.355173, lat: 36.082982, population: 1007, area: 11293, gdp: 12400, features: '海滨城市，啤酒之都' },
  { id: 3, name: '烟台市', lng: 121.391382, lat: 37.539297, population: 710, area: 13746, gdp: 7816, features: '港口城市，苹果之乡' },
  { id: 4, name: '潍坊市', lng: 119.107078, lat: 36.70925, population: 939, area: 16143, gdp: 5872, features: '风筝之都，农业大市' },
  { id: 5, name: '临沂市', lng: 118.326443, lat: 35.065282, population: 1102, area: 17191, gdp: 4805, features: '革命老区，物流之都' },
  { id: 6, name: '淄博市', lng: 118.054926, lat: 36.813487, population: 470, area: 5965, gdp: 4200, features: '工业重镇，陶瓷之都' },
  { id: 7, name: '济宁市', lng: 116.587245, lat: 35.415393, population: 835, area: 11187, gdp: 4494, features: '孔孟之乡，运河之都' },
  { id: 8, name: '泰安市', lng: 117.129063, lat: 36.194968, population: 549, area: 7762, gdp: 2664, features: '泰山脚下，旅游城市' },
  { id: 9, name: '威海市', lng: 122.116394, lat: 37.509691, population: 291, area: 5797, gdp: 3017, features: '海滨城市，宜居城市' },
  { id: 10, name: '德州市', lng: 116.307428, lat: 37.453968, population: 561, area: 10356, gdp: 3075, features: '鲁西北重镇，交通枢纽' },
  { id: 11, name: '聊城市', lng: 115.980367, lat: 36.456013, population: 592, area: 8715, gdp: 2316, features: '江北水城，运河古城' },
  { id: 12, name: '滨州市', lng: 117.970803, lat: 37.383542, population: 393, area: 9600, gdp: 2587, features: '黄河三角洲，纺织之都' },
  { id: 13, name: '菏泽市', lng: 115.469381, lat: 35.246531, population: 878, area: 12238, gdp: 3483, features: '牡丹之都，农业大市' },
  { id: 14, name: '枣庄市', lng: 117.557964, lat: 34.810976, population: 386, area: 4563, gdp: 1730, features: '煤城，台儿庄古城' },
  { id: 15, name: '东营市', lng: 118.4963, lat: 37.461266, population: 220, area: 8243, gdp: 2981, features: '石油之城，黄河入海口' },
  { id: 16, name: '日照市', lng: 119.526888, lat: 35.416377, population: 296, area: 5359, gdp: 2006, features: '海滨城市，港口城市' }
]

// 百度地图API加载状态
let baiduMapLoadPromise: Promise<void> | null = null
let baiduMapLoadResolve: (() => void) | null = null
let baiduMapLoadReject: ((error: Error) => void) | null = null

// 全局回调函数
declare global {
  interface Window {
    initBaiduMapCallback: () => void
  }
}

// 设置全局回调函数
if (typeof window !== 'undefined') {
  window.initBaiduMapCallback = () => {
    console.log('百度地图API回调函数被调用')
    // 延迟一点时间确保API完全初始化
    setTimeout(() => {
      console.log('检查百度地图API是否完全加载...')
      if (isBaiduMapLoaded()) {
        console.log('百度地图API加载完成')
        if (baiduMapLoadResolve) {
          baiduMapLoadResolve()
        }
      } else {
        console.warn('百度地图API回调被调用但API未完全加载')
      }
    }, 200)
  }
}

// 检查百度地图API是否已加载
export const isBaiduMapLoaded = (): boolean => {
  return typeof window !== 'undefined' && 
         typeof (window as any).BMap !== 'undefined' && 
         typeof (window as any).BMap.Map !== 'undefined' &&
         typeof (window as any).BMap.MapTypeId !== 'undefined'
}

// 等待百度地图API加载完成
export const waitForBaiduMap = (): Promise<void> => {
  console.log('开始等待百度地图API加载...')
  
  // 如果已经加载完成，直接返回
  if (isBaiduMapLoaded()) {
    console.log('百度地图API已加载完成')
    return Promise.resolve()
  }
  
  // 如果已经有加载中的Promise，返回它
  if (baiduMapLoadPromise) {
    console.log('百度地图API正在加载中，等待现有Promise...')
    return baiduMapLoadPromise
  }
  
  // 创建新的加载Promise
  baiduMapLoadPromise = new Promise<void>((resolve, reject) => {
    baiduMapLoadResolve = resolve
    baiduMapLoadReject = reject
    
    console.log('创建百度地图API加载Promise')
    
    // 备用轮询检查
    let pollCount = 0
    const maxPolls = 100 // 10秒轮询 (100 * 100ms)
    const pollInterval = setInterval(() => {
      pollCount++
      console.log(`轮询检查百度地图API加载状态 (${pollCount}/${maxPolls})`)
      
      if (isBaiduMapLoaded()) {
        console.log('百度地图API通过轮询检查加载完成')
        clearInterval(pollInterval)
        clearTimeout(timeout)
        resolve()
        baiduMapLoadPromise = null
        baiduMapLoadResolve = null
        baiduMapLoadReject = null
      } else if (pollCount >= maxPolls) {
        console.error('百度地图API轮询检查超时')
        clearInterval(pollInterval)
        clearTimeout(timeout)
        reject(new Error('百度地图API加载超时，请检查网络连接'))
        baiduMapLoadPromise = null
        baiduMapLoadResolve = null
        baiduMapLoadReject = null
      }
    }, 100)
    
    // 设置超时
    const timeout = setTimeout(() => {
      console.error('百度地图API加载超时')
      clearInterval(pollInterval)
      if (baiduMapLoadReject) {
        baiduMapLoadReject(new Error('百度地图API加载超时，请检查网络连接'))
        baiduMapLoadPromise = null
        baiduMapLoadResolve = null
        baiduMapLoadReject = null
      }
    }, 15000) // 15秒超时
    
    // 如果API已经加载，立即解决
    if (isBaiduMapLoaded()) {
      console.log('百度地图API在检查时已加载完成')
      clearInterval(pollInterval)
      clearTimeout(timeout)
      resolve()
      baiduMapLoadPromise = null
      baiduMapLoadResolve = null
      baiduMapLoadReject = null
    }
  })
  
  return baiduMapLoadPromise
}
